﻿using MyX.Core;
using MyX.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyX.Work
{
    public class XWorkRun<T> : XIWorkRun where T : class, XIMessage
    {

        public XWorkRun(Action<T> action)
        {
            _action = action;
        }

        public bool Filter(object arg)
        {
            return arg as T != null;
        }

        public void Do(object arg)
        {
            var inputArg = arg as T;
            _action(inputArg);            
        }

        private Action<T> _action;
    }

    public class XWorkStatusRun<T> : XIWorkRun where T : XWorkStatus
    {
        public XWorkStatusRun(Action<T> action)
        {
            _action = action;
        }

        public bool Filter(object arg)
        {
            return arg as T != null;
        }

        public void Do(object arg)
        {
            var inputArg = arg as T;
            try
            {
                _action(inputArg);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private Action<T> _action;
    }

    public class XWorkResultRun<T> : XIWorkRun where T : XWorkResult
    {
        public XWorkResultRun(Action<T> action)
        {
            _action = action;
        }

        public bool Filter(object arg)
        {
            return arg as T != null;
        }

        public void Do(object arg)
        {
            var inputArg = arg as T;
            try
            {
                _action(inputArg);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private Action<T> _action;
    }
}
